package com.bjtg.threegoclinet.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;

import com.bjfl.saleManager.util.LogUtil;
import com.log.LatteLogger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by 86463
 * Created time: 2018/3/15/14:01
 */

public class AtuoDeleteLogService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
 private ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LatteLogger.d("AtuoDelLogSer start","start");
       executorService.execute(() -> {
            List<String> dirList = new ArrayList<String>(Arrays.asList(LogUtil.ELELOCK_LOG, LogUtil.GOODS_DEVICE_LOG,
                    LogUtil.EXCEPTION_LOG));
            for (String dirStr : dirList) {
                LogUtil.deleteFile(dirStr);
            }
        });
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int repeatPeriod = 30 * 24 * 60 * 60 * 1000; //30 天
        long triggerAtTime = SystemClock.elapsedRealtime() + repeatPeriod;
        Intent i = new Intent(this, AutoUpdateService.class);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        manager.cancel(pi);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return super.onStartCommand(intent, flags, startId);
    }
}
